package com.roast.services.my;

import com.roast.util.QueryObject;
import java.util.ArrayList;
import com.roast.domain.*;

public class CommentAddressingIMPL extends GeneralSO implements CommentAddressing {

    private Address address;
    private Comment comment;
    private QueryObject queryObject;
    private DomainObject fromDB;

    @Override
    public int comment(Address address) {
        return new CommentAddressingIMPL().generalExecution(address);
    }

    @Override
    protected int executeSO(Object obj) {
        if (!setAddress(obj)) {
            return 10801;
        }
        if (!setComment()) {
            return 10802;
        }
        setQueryObject();
        if (!readAddress()) {
            return 10803;
        }
        if (!addCommentToAddress()) {
            return 10804;
        }
        if (!copyObject(obj)) {
            return 10805;
        }

        return 10800;
    }

    private boolean setAddress(Object obj) {
        try {
            this.address = (Address) obj;
            return true;
        } catch (ClassCastException ex) {
            return false;
        }
    }

    private boolean setComment() {
        ArrayList<Comment> commentList;
        try {
            commentList = (ArrayList<Comment>) address.getCommentList();
        } catch (ClassCastException ex) {
            return false;
        }

        if (commentList == null) {
            return false;
        }

        try {
            comment = commentList.remove(0);
            return comment != null;
        } catch (IndexOutOfBoundsException ex) {
            return false;
        }
    }

    private void setQueryObject() {
        queryObject = new QueryObject();
        queryObject.setDomaninObject(address);
    }

    private boolean readAddress() {
        fromDB = dbbr.readOneByID(queryObject);
        return (queryObject.getSignal() == 10020);
    }

    private boolean addCommentToAddress() {
        try {
            ((Address) fromDB).getCommentList().add(comment);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    private boolean copyObject(Object obj) {
        try {
            return ((Address) obj).copyObject(fromDB);
        } catch (ClassCastException ex) {
            return false;
        }
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 10800:
                showOperationState(signal, "commentAddressing: You have successfully comment addressing.");
                return true;
            case 10801:
                showOperationState(signal, "commentAddressing:ERROR: Class cast exception.");
                return false;
            case 10802:
                showOperationState(signal, "commentAddressing:ERROR: You fail set comment.");
                return false;
            case 10803:
                showOperationState(signal, "commentAddressing:ERROR: You fail to read address from database.");
                return false;
            case 10804:
                showOperationState(signal, "commentAddressing:ERROR: You fail comment addressing.");
                return false;
            case 10805:
                showOperationState(signal, "commentAddressing:ERROR: Fail to copy object.");
                return false;
        }
        return false;
    }

}
